var initTime = null;
var leftMenuInit = false;
var topMenuInit = false;
var rightMenuInit = false;
var historyTab = [];
var contextTitle = null;
$(function(){
	initTime =window.setInterval( "initEvent()" , 100 );
	drawLayout(); 
	initTab();
	
	$( document.body ).layout( "panel"  , "west" ).panel( {
		onResize:function(){
			$("#aa").accordion( "resize" );
		}
	});
});

function drawLayout(){
	$(document.body).layout("collapse","east");
}
function refreshRegion( region ){
	$(document.body).layout("panel", region ).panel("refresh");
}
function openBookmark(){
	$(document.body).layout("expand","east");
}
/** menu navigation click event */
function initEvent(){
	if( leftMenuInit && topMenuInit && rightMenuInit && initTime ){
		treeEvent( ".easyui-tree" );
		$(".menu-item").click(function(){
			var path = $(this).attr( "id" );
			if( path != "" ){
				var target = $( "[node-id=" + path + "]" );
				$(".easyui-tree").each(function(){
					var pTree = $(this);
					if( pTree.attr( "id" ) != "bookmarkUL" ){
						var node = pTree.tree("find" , path );
						if( node ){
							pTree.tree( "select" , node.target );
							pTree.tree( "expandTo" , node.target );
							$("#aa").accordion( "select" , pTree.attr("title") );
						}
					}
				});
			}
		});
		window.clearInterval( initTime );
	}
}

function treeEvent( selector ){
	$( selector ).tree({
		onSelect:function(node){
			var title = $(node.text).html();
			var path = node.id;
			if( path ){
				if( path.lastIndexOf( ".ht" ) < 0 && path.lastIndexOf( "http://" ) < 0 ){
					path = path + ".ht";
				}
				addTab( title , path );
			}
		}
	});
}

function initTab(){
	$('#tabContent').tabs({
		fit:true,
		border:false,
		onBeforeClose:function( title ){
			try{
				var tab = $('#tabContent').tabs( "getTab" , title );
				var iframe = getFrameDocument( $(tab).children( "iframe" ).attr("id") );
				var url = iframe.location.href;
				historyTab.push( {title:title , path : url } );
			}catch(e){}
		},
		onContextMenu:function(e , title ){
			contextTitle = title;
			$("#tabContent").tabs("select",title );
			$('#contextmenu').menu('show', {left: e.pageX,top: e.pageY});
		},
		tools:[{
		iconCls:'icon-add',
		handler: newTab
	}]});
	$( "#tabContent" ).bind('contextmenu',function(e){return false;});
	addTab( "首页" , "about:blank" );
}
var newTabIndex = 1;
function addTab( text , url ){
	if( text == 'new' ){
		text = "新建窗口" + newTabIndex;
		newTabIndex++;
	}
	if( $('#tabContent').tabs("exists" , text ) ){
		$('#tabContent').tabs("select" , text );
		return;
	}
	$('#tabContent').tabs('add',{
		title: text,
		content:'<iframe id="' + text + '" src="' + url + '" frameborder="0" style="width:100%;height:100%;"></iframe>',
		iconCls:'icon-file',
		closable:true
	});
}
function newTab(){
	$.messager.prompt('新建窗口', '请输入要访问的网址', function(r){
		if (r){addTab( 'new' ,  r );}
	});
	$(".messager-input").val( "http://" );
}

/**关闭窗口
* @param isOther true , 关闭其他窗口,false , 关闭当前窗口 
*/
function closeTab( isOther ){
	if( isOther ){
		$(".tabs-title").each(function(){
			var title = $(this).html();
			if( contextTitle != title ){
				$("#tabContent").tabs("close",title );
			}
		});
	}else{
		$("#tabContent").tabs("close",contextTitle );
	}
}
function reloadTab(){
	var tab = historyTab.pop();
	if( tab ){
		addTab( tab.title , tab.path );
	}
}

function refreshTab( tabTitle ){
	var tab = null;
	if( tabTitle ){
		tab = $("#tabContent").tabs("getTab",tabTitle );
	}else{
		tab = $("#tabContent").tabs("getTab",contextTitle );
	}
	var iframe = getFrameDocument( $(tab).children( "iframe" ).attr("id") );
	iframe.location.href = iframe.location.href;
}
function saveBookmark(){
	var content = $("#tabContent").tabs("getTab",contextTitle ).html();
	var path = $( content ).attr( "src" );
	var param = "name=" + contextTitle + "&url=" + path;
	$.ajax({
			url:"bookmark/save.ht",
			data:param,
			type:'post',
		    dataType:'json',
		    success:function(flag){
			if( flag ){
				infoMsg("提示","保存到收藏夹成功");
				refreshRegion( "east" );
			}else
				errorMsg( "提示","保存到收藏夹失败!" );
		}});
}